package com.manager.plat.action;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.security.NoSuchAlgorithmException;
import java.util.List;

import org.apache.commons.lang.exception.ExceptionUtils;
import org.apache.log4j.Logger;

import com.manager.plat.dao.IUserInfoDao;
import com.manager.plat.util.BaseUtil;
import com.manager.plat.vo.UserInfoVo;
import com.opensymphony.xwork2.ActionSupport;

public class UserInfoAction extends ActionSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = 2774149075315195332L;

	private Logger logger = Logger.getLogger(UserInfoAction.class);
	
	private IUserInfoDao userInfoDao;		//用户信息数据
	private List<UserInfoVo> userInfoVoList;//用户信息列表
	private UserInfoVo userInfoVo;			//用户信息VO
	private String actionPath;				//转发路径地址
	
	/**获得所有的用户列表*/
	public String getUserInfoList(){
		userInfoVoList = userInfoDao.getUserInfoList();
		return SUCCESS;
	}
	/**新增用户信息*/
	public String addUserInfo(){
		userInfoVo = null;
		return SUCCESS;
	}
	/**编辑用户信息*/
	public String editUserInfo(){
		if(userInfoVo.getId()  != 0){
			userInfoVo = userInfoDao.getUserInfoById(userInfoVo.getId());
		}
		return SUCCESS;
	}
	/**更新用户信息*/
	public String updateUserInfo(){
		/**获得用户名*/
		String login_username = userInfoVo.getLogin_username();
		/**获得用户密码*/
		String login_password = userInfoVo.getLogin_password();
		/**根据用户名和密码生成密匙*/
		try {
			String user_key = BaseUtil.md5(login_username + login_password);
			userInfoVo.setUser_key(user_key);
		} catch (NoSuchAlgorithmException e) {
			logger.error(String.format("获得密匙异常:%s", ExceptionUtils.getFullStackTrace(e)));
		}
		
		if(userInfoVo.getId()  == 0){
			/**插入用户信息*/
			int count = userInfoDao.insertUserInfo(userInfoVo);
			if(count > 0){
				logger.info(String.format("[%s]用户添加成功", userInfoVo.getLogin_username()));
			}else{
				logger.info(String.format("[%s]用户添加失败", userInfoVo.getLogin_username()));
			}
		}else {
			/**更新用户信息*/
			int count =userInfoDao.updateUserInfo(userInfoVo);
			if(count > 0){
				logger.info(String.format("[%s]用户更新成功", userInfoVo.getLogin_username()));
			}else{
				logger.info(String.format("[%s]用户更新失败", userInfoVo.getLogin_username()));
			}
		}
		actionPath = "userInfo/getUserInfoList.action";
		return SUCCESS;
	}
	/**删除用户信息*/
	public String deleteUserInfo(){
		try {
			userInfoVo.setLogin_username(URLDecoder.decode(userInfoVo.getLogin_username(), "UTF-8"));
		} catch (UnsupportedEncodingException e) {
			logger.error(String.format("删除用户信息解码异常:%s", ExceptionUtils.getFullStackTrace(e)));
		}
		
		int count = userInfoDao.deleteUserInfo(userInfoVo.getId());
		if(count > 0){
			logger.info(String.format("[%s]删除用户成功", userInfoVo.getLogin_username()));
		}else{
			logger.info(String.format("[%s]删除用户失败", userInfoVo.getLogin_username()));
		}
		
		actionPath = "userInfo/getUserInfoList.action";
		return SUCCESS;
	}

	
	public IUserInfoDao getUserInfoDao() {
		return userInfoDao;
	}
	public void setUserInfoDao(IUserInfoDao userInfoDao) {
		this.userInfoDao = userInfoDao;
	}
	public List<UserInfoVo> getUserInfoVoList() {
		return userInfoVoList;
	}
	public void setUserInfoVoList(List<UserInfoVo> userInfoVoList) {
		this.userInfoVoList = userInfoVoList;
	}
	public UserInfoVo getUserInfoVo() {
		return userInfoVo;
	}
	public void setUserInfoVo(UserInfoVo userInfoVo) {
		this.userInfoVo = userInfoVo;
	}
	public String getActionPath() {
		return actionPath;
	}
	public void setActionPath(String actionPath) {
		this.actionPath = actionPath;
	}
}
